package com.iqiyi.pps.epg.core.rpc.news;

import com.iqiyi.pps.epg.api.model.web.news.TNewsDetailsData;
import com.iqiyi.pps.epg.api.model.web.news.TNewsDetailsDataPage;
import com.iqiyi.pps.epg.api.model.web.news.TStatisticNewsDetailsDataPage;
import com.iqiyi.pps.epg.api.model.web.news.TStatistiticNewsDetailsDataPage;
import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.server.web.news.NewsWebService;
import com.iqiyi.pps.epg.core.model.news.NewsDetails;
import com.iqiyi.pps.epg.core.service.news.NewsDetailsDataService;
import com.iqiyi.pps.epg.core.service.news.StatisticNewsDetailsDataService;
import com.iqiyi.pps.epg.core.utils.ApplicationContextUtil;
import com.iqiyi.pps.epg.core.utils.StringUtil;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by zhang on 2016/11/29.
 */
public class NewsWebServiceImpl implements NewsWebService.Iface {

    public static Logger logger = LoggerFactory.getLogger(NewsWebServiceImpl.class);

    @Override
    public boolean saveNewsDetailsData(TNewsDetailsData entity) throws TException {
        if (null == entity)
            return false;

        try {
            NewsDetails newsDetails = new NewsDetails();
            NewsDetailsDataService newsDetailsDataService = (NewsDetailsDataService) ApplicationContextUtil.getBean(NewsDetailsDataService.class);
            newsDetails.setNewsName(entity.getNewsName());
            newsDetails.setChannelId(entity.getChannelId());
            newsDetails.setChannelName(entity.getChannelName());
            newsDetails.setServerRegion(entity.getServerRegion());
            newsDetails.setServerIp(entity.getServerIp());
            newsDetails.setLegoProgramId(entity.getLegoProgramId());
            newsDetails.setNewsProgramId(entity.getNewProgramId());
            newsDetails.setProductTime(Timestamp.valueOf(entity.getProductTime()));
            newsDetails.setStatus(entity.getStatus());
            newsDetails.setIntelligentTitle(entity.getIntelligentTitle());
            newsDetails.setSubmitTitle(entity.getSubmitTitle());
            List<Integer> list = StringUtil.alikePercentList(entity.getIntelligentTitle(), entity.getSubmitTitle());
            newsDetails.setPercentTitle(list.get(0));
            newsDetails.setCorrectWordNum(list.get(1));
            newsDetails.setErrorWordNum(list.get(2));
            newsDetails.setIntelligentStartNum(entity.getIntelligentStartNum());
            newsDetails.setSubmitStartNum(entity.getSubmitStartNum());
            newsDetails.setErrorStartNum(entity.getIntelligentStartNum() - entity.getSubmitStartNum());
            newsDetails.setIntelligentEndNum(entity.getIntelligentEndNum());
            newsDetails.setSubmitEndNum(entity.getSubmitEndNum());
            newsDetails.setErrorEndNum(entity.getIntelligentEndNum() - entity.getSubmitEndNum());
            newsDetails.setExcLog(entity.getExcLog());
            newsDetails.setSubmitUser(entity.getSubmitUser());
            newsDetails.setCostTime(entity.getCostTime());  // TODO 数据不确定
            newsDetailsDataService.saveNewsDetails(newsDetails);
        } catch (Exception e) {
            logger.error("[NewsWebServiceImpl][saveNewsDetailsData]", e);
            return false;
        }

        return true;
    }

    @Override
    public TNewsDetailsDataPage getNewsDetailsData(TPage page, TFilter filter) throws TException {
        NewsDetailsDataService service = (NewsDetailsDataService) ApplicationContextUtil.getBean(NewsDetailsDataService.class);
        return service.findTNewsDetailsDataPage(page, filter);
    }

    @Override
    public TStatisticNewsDetailsDataPage getStatisticNewsDetailsData(TNewsDetailsDataPage entity) throws TException {
        StatisticNewsDetailsDataService service = (StatisticNewsDetailsDataService) ApplicationContextUtil.getBean(StatisticNewsDetailsDataService.class);
        return service.statisticNewsDetailsData(entity);
    }
}
